import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions

driver = webdriver.Chrome()
url = "https://www.douban.com"
driver.get(url)

# 点击事件之前获取所有的句柄，然后传入显性等待
all_handles = driver.window_handles
driver.find_element(By.XPATH, "//a[@class='lnk-book']").click()

#打印所有句柄
print(driver.window_handles)
# 切换窗口，参数为句柄的最后一个，这里的参数是索引0123456
driver.switch_to.window(driver.window_handles[1])
# time.sleep(2)
# 使用显性等待方法
wait = WebDriverWait(driver, 2)
wait.until(expected_conditions.new_window_is_opened(all_handles))

print(driver.title)
driver.quit()
